USE maindatabase

if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmpComeralInv]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[tmpComeralInv] (
	[idRecord] [int] IDENTITY (1, 1) NOT NULL ,
	[idPredpr] [bigint] NULL ,
	[invoicefullname] [varchar] (25) COLLATE Cyrillic_General_CI_AS NULL ,
	[datetrans] [datetime] NULL ,
	[status] [tinyint] NULL ,
	[recipientname] [varchar] (200) COLLATE Cyrillic_General_CI_AS NULL ,
	[signerfio] [varchar] (70) COLLATE Cyrillic_General_CI_AS NULL ,
	[namepredpr] [varchar] (300) COLLATE Cyrillic_General_CI_AS NULL ,
	[docnumber] [varchar] (50) COLLATE Cyrillic_General_CI_AS NULL ,
	[docdate] [datetime] NULL ,
	[ndsexcel] [decimal](18, 3) NULL ,
	[ndsspr] [decimal](18, 3) NULL ,
	[ndsinvoice] [decimal](18, 3) NULL ,
	[totalcostinv] [decimal](18, 3) NULL ,
	[totalcostvatinv] [decimal](18, 3) NULL ,
	[filial] [smallint] NULL ,
	[svplace] [varchar] (1) COLLATE Cyrillic_General_CI_AS NULL ,
	[numbuhspr] [varchar] (10) COLLATE Cyrillic_General_CI_AS NULL ,
	[numdocpart] [varchar] (50) COLLATE Cyrillic_General_CI_AS NULL ,
	[hide] [bit] NULL
) ON [PRIMARY]

ALTER TABLE [dbo].[tmpComeralInv] WITH NOCHECK ADD 
	CONSTRAINT [PK_tmpComeralInv] PRIMARY KEY  CLUSTERED 
	(
		[idRecord]
	)  ON [PRIMARY] 

ALTER TABLE [dbo].[tmpComeralInv] ADD 
	CONSTRAINT [DF_tmpComeralInv_hide] DEFAULT (0) FOR [hide]
END


if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tmpComeralProv]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[tmpComeralProv] (
	[idRecord] [int] IDENTITY (1, 1) NOT NULL ,
	[numbuhspr] [varchar] (10) COLLATE Cyrillic_General_CI_AS NULL ,
	[datadoc] [datetime] NULL ,
	[numdoc] [varchar] (50) COLLATE Cyrillic_General_CI_AS NULL ,
	[numdocpart] [varchar] (50) COLLATE Cyrillic_General_CI_AS NULL ,
	[summ] [decimal](18, 3) NULL ,
	[globalcode] [varchar] (50) COLLATE Cyrillic_General_CI_AS NULL ,
	[filial] [smallint] NULL ,
	[NSF] [varchar] (30) COLLATE Cyrillic_General_CI_AS NULL ,
	[mes] [datetime] NULL 	
) ON [PRIMARY]

ALTER TABLE [dbo].[tmpComeralProv] WITH NOCHECK ADD 
	CONSTRAINT [PK_tmpComeralProv] PRIMARY KEY  CLUSTERED 
	(
		[idRecord]
	)  ON [PRIMARY] 
END

if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ComeralSigners]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[ComeralSigners] (
	[idRecord] [int] IDENTITY (1, 1) NOT NULL ,
	[filial] [smallint] NOT NULL ,
	[fio] [varchar] (100) COLLATE Cyrillic_General_CI_AS NOT NULL 
) ON [PRIMARY]

ALTER TABLE [dbo].[ComeralSigners] ADD 
	CONSTRAINT [PK_ComeralSigners] PRIMARY KEY  CLUSTERED 
	(
		[idRecord]
	)  ON [PRIMARY] ,
	CONSTRAINT [IX_ComeralSigners] UNIQUE  NONCLUSTERED 
	(
		[filial],
		[fio]
	)  ON [PRIMARY] 
END

if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ComeralZeroExclude]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[ComeralZeroExclude] (
	[idRecord] [int] IDENTITY (1, 1) NOT NULL ,
	[filial] [smallint] NOT NULL ,
	[invoice] [varchar] (25) COLLATE Cyrillic_General_CI_AS NOT NULL 
) ON [PRIMARY]

ALTER TABLE [dbo].[ComeralZeroExclude] ADD 
	CONSTRAINT [PK_ComeralZeroExclude] PRIMARY KEY  CLUSTERED 
	(
		[idRecord]
	)  ON [PRIMARY] 
END

